<?php

namespace app\admin\controller\work_area;

use app\admin\service\work_area\BusinessService;
use app\common\controller\Backstage;

use app\common\model\work_area\HkDeviceSerial;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("录像机管理")
 */
class DeviceSerial extends Backstage
{
    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['info']
        ]
    ];

    /**
     * @Apidoc\Title("列表")
     * @Apidoc\Url("/admin/work_area.DeviceSerial/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        try {
            $param = $this->request->param();
            $res = BusinessService::deviceSerialList($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '成功', $res);
    }


    /**
     * @Apidoc\Title("详情")
     * @Apidoc\Url("/admin/work_area.DeviceSerial/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="劳务单位id")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("name", type="string", desc="工区名称"),
     *    @Apidoc\Returned("work_area_stand_id", type="int", desc="站点id"),
     *    @Apidoc\Returned("sort", type="int", desc="排序"),
     *    @Apidoc\Returned("remarks", type="string", desc="备注"),
     *    @Apidoc\Returned("yj_name", type="string", desc="云鲸名称"),
     * )
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            $res = BusinessService::deviceSerialInfo($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '获取成功', $res);
    }

    /**
     * @Apidoc\Title("添加,修改")
     * @Apidoc\Url("/admin/work_area.DeviceSerial/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int", desc="修改传id")
     * @Apidoc\Param("device_rod_id",type="int",require=true, desc="装置id")
     * @Apidoc\Param("name",type="string",require=true, desc="名称")
     * @Apidoc\Param("comply_with",type="string",require=true, desc="语音遵守内容")
     * @Apidoc\Param("device_serial",type="string",require=true, desc="设备编号")
     * @Apidoc\Param("work_area_stand_id", type="int",require=true, desc="站点id")
     * @Apidoc\Param("rtp_type", type="int",require=true, desc="1海康 2流媒体")
     * @Apidoc\Param("rtp_url_whole", type="string",require=true, desc="流媒体全地址")
     * @Apidoc\Param("sort", type="int", desc="排序")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Param("yj_name", type="string", desc="云鲸名称")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function edit()
    {
        try {
            $param = $this->request->param();
            $result = JWTAuth::auth();
            $admin_id = $result['admin_id'];
            $data = [
                'name|名称' => 'require',
                'comply_with|语音遵守内容' => 'require',
                'device_serial|设备编号' => 'require',
                'work_area_stand_id|站点id' => 'require',
                'rtp_url_whole|流媒体全地址' => 'require'
            ];
            Validate($data)->check($param);
            BusinessService::deviceSerialSave($param, $admin_id);
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $admin_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }

    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/admin/work_area.DeviceSerial/del")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="id")
     * @Apidoc\Returned("data", type="object", desc="对象信息")
     */
    public function del()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            HkDeviceSerial::destroy($param['id']);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }

}